#include <QMenu>
#include <QToolButton>
#include <QLabel>
#include <QWidget>
#include <QPalette>
#include <QBrush>
#include <QListWidget>

#include "guiplugin1.h"

#include <plugin/plugin/PluginFactory.h>
#include <plugin/util/Framework.h>
#include <pluginui/manager/AbstractMenuManager.h>
#include <pluginui/manager/AbstractToolBarManager.h>
#include <pluginui/manager/AbstractStatusBarManager.h>
#include <pluginui/manager/AbstractPanelManager.h>
#include <pluginui/manager/AbstractPanelContainer.h>
#include <pluginui/manager/ConfigPanelManager.h>

#include "GuiPlugin1ConfigPanel.h"

using namespace antu::plugin;
using namespace antu::plugin::ui;

Guiplugin1::Guiplugin1(PluginDescriptionSharedPtr pDescription, ServiceRegistry *pServiceRegistry)
    : GuiPlugin(pDescription, pServiceRegistry)
{

}

void Guiplugin1::doStartMenu(AbstractMenuManager *pMenuManager)
{
    QMenu* pFileMenu = pMenuManager->menu("fileMenu", "&File");
    pFileMenu->addAction("Open");
    pFileMenu->addSeparator();
    pFileMenu->addAction("Close");
}

void Guiplugin1::doStartToolBar(AbstractToolBarManager *pToolBarManager)
{
    AbstractToolBar* pBar = pToolBarManager->toolBar("test");

    QToolButton* pButton1 = new QToolButton();
    pButton1->setText("Test1");
    pButton1->setToolButtonStyle(Qt::ToolButtonTextOnly);
    pBar->addWidget(pButton1);
    pBar->addSeparator();

    QToolButton* pButton2 = new QToolButton();
    pButton2->setText("Test2");
    pButton2->setToolButtonStyle(Qt::ToolButtonTextOnly);
    pBar->addWidget(pButton2);
}

void Guiplugin1::doStartContentPanel(AbstractPanelManager *pContentPanelManager)
{
    QWidget* pWidget = new QWidget();
    QPalette palette;
    palette.setBrush(QPalette::Window,QBrush(QColor(250,0,0,150)));
    pWidget->setPalette(palette);
    pWidget->setAutoFillBackground(true);

    AbstractPanelContainer* pContainer = pContentPanelManager->container("Test", "Test 1");
    pContainer->addPanel("Test", pWidget);

    QWidget* pWidget2 = new QWidget();
    QPalette palette2;
    palette2.setBrush(QPalette::Window,QBrush(QColor(0,0,250,150)));
    pWidget2->setPalette(palette2);
    pWidget2->setAutoFillBackground(true);

    pContainer = pContentPanelManager->container("Test2", "Test 2");
    pContainer->addPanel("Test2", pWidget2);
}

void Guiplugin1::doStartSidePanel(AbstractPanelManager *pSidePanelManager)
{
    QListWidget* pWidget1 = new QListWidget();
    pWidget1->addItem("1");
    pWidget1->addItem("2");
    pWidget1->addItem("3");
    AbstractPanelContainer* pContainer = pSidePanelManager->container("Test", "Test");
    pContainer->addPanel("List", pWidget1);

    QListWidget* pWidget2 = new QListWidget();
    pWidget2->addItem("2");
    pWidget2->addItem("3");
    pWidget2->addItem("4");
    pContainer = pSidePanelManager->container("Test2", "Test 2", AbstractPanelManager::PT_Tab, Qt::NoDockWidgetArea);
    pContainer->addPanel("List2", pWidget2);

    QListWidget* pWidget3 = new QListWidget();
    pWidget3->addItem("3");
    pWidget3->addItem("4");
    pWidget3->addItem("5");
    pWidget3->setWindowTitle("List 2");
    pContainer = pSidePanelManager->container("Test3", "Test 3", AbstractPanelManager::PT_Tab);
    pContainer->addPanel("List3", pWidget3);
    pContainer->setChangeWindowTitleWithTab(true);
}

void Guiplugin1::doStartStatusBar(AbstractStatusBarManager *pStatusBarManager)
{
    QLabel* pLabel = new QLabel();
    pLabel->setText("Test Message");
    pStatusBarManager->appendPermanentStatusBarItem(pLabel);
}

void Guiplugin1::doInit(PluginContext *pContext)
{

}

void Guiplugin1::doStart()
{
    Framework::Instance.logger()->debug(__FILE__, __LINE__, "Guiplugin1", "doStart", "starting");
    ConfigPanelManager* pManager = qobject_cast<ConfigPanelManager*>(this->context()->contextObjectAt(ConfigPanelManager::Name));

    if (pManager != nullptr)
    {
        pManager->addItem(QIcon(":/resources/images/book.png"), "Test Config", new GuiPlugin1ConfigPanel);
    }
    Framework::Instance.logger()->debug(__FILE__, __LINE__, "Guiplugin1", "doStart", "started");
}

void Guiplugin1::doStop()
{
}

void Guiplugin1::doFinishStartup()
{

}

void Guiplugin1::requiredServiceFound(AbstractService *pService)
{

}


antu::plugin::AbstractPlugin* InstantiatePlugin(antu::plugin::PluginDescriptionSharedPtr& description,
                                                antu::plugin::ServiceRegistry* pServiceRegistry)
{
    return new Guiplugin1(description, pServiceRegistry);
}

void InitFactory(const QString &code)
{
    antu::plugin::PluginFactory::Factory.registerPluginInfo(
                code,
                InstantiatePlugin);
}
